from apps.meiduo_admin.serializers.user import UserSerializer
from rest_framework.generics import ListCreateAPIView
from apps.users.models import User
from apps.meiduo_admin.utils import PageNum


class UserListView(ListCreateAPIView):

    serializer_class = UserSerializer

    # 使用自定义分页类
    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword is '' or keyword is None:
            return User.objects.all().order_by('id')
        else:
            return User.objects.filter(username__contains=keyword).order_by('id')
